/**
 * Copyright (c) 2014-2015 中维云web端团队   All rights reserved
 * <p>
 * Base on awframework,powered by aiwei web team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package cn.y.operation.framework.utils.system;

import cn.y.operation.framework.utils.spring.SpringContextHolder;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * @Description: 缓存工具类
 * @author Chenz
 * @date Nov 13, 2014 1:10:22 PM 
 * @version V1.0
 */
public class CacheUtils {

    private static final CacheManager cacheManager = SpringContextHolder.getBean("cacheManagerFactory");

    private static final String SYS_CACHE = "sysCache";

    /**
     * 获取系统缓存
     */
    public static Object get(String key) {
        return get(SYS_CACHE, key);
    }

    public static void put(String key, Object value) {
        put(SYS_CACHE, key, value);
    }

    public static void remove(String key) {
        remove(SYS_CACHE, key);
    }

    public static Object get(String cacheName, String key) {
        Element element = getCache(cacheName).get(key);
        return element == null ? null : element.getObjectValue();
    }

    public static void put(String cacheName, String key, Object value) {
        Element element = new Element(key, value);
        getCache(cacheName).put(element);
    }

    public static void remove(String cacheName, String key) {
        getCache(cacheName).remove(key);
    }

    /**
     * 获得一个Cache，没有则创建一个。
     */
    private static Cache getCache(String cacheName) {
        Cache cache = cacheManager.getCache(cacheName);
        if (cache == null) {
            cacheManager.addCache(cacheName);
            cache = cacheManager.getCache(cacheName);
            cache.getCacheConfiguration().setEternal(true);
        }
        return cache;
    }

    public static CacheManager getCacheManager() {
        return cacheManager;
    }

}
